package com.heer.gymnasium.jpush;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.heer.gymnasium.constant.Extras;
import com.heer.gymnasium.module.article.views.ArticleActivity;
import com.heer.gymnasium.module.main.views.FindFragment;
import com.heer.gymnasium.module.message.views.MessageListActivity;
import com.heer.gymnasium.module.store.views.StoreActivity;
import com.zhihuiyun.library.utils.L;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

import static com.heer.gymnasium.constant.Extras.EXTRA_ID;
import static com.heer.gymnasium.constant.Extras.EXTRA_TYPE;

/**
 * Created by yan
 * on 2018/3/23
 */

public class MyReceiver extends BroadcastReceiver {

    private static final String TAG = MyReceiver.class.getName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        L.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + bundle);
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            L.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            L.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            L.e(TAG, "[MyReceiver] 接收到推送下来的自定义通知: ");
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            L.e(TAG + "自定义字段sss", extra);
            try {
                JSONObject jsonobject = new JSONObject(extra);
                String type = jsonobject.getString("type");
                if (type.equals("1")) {
                    Intent newIntent = new Intent(context, MessageListActivity.class);
                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(newIntent);
                }
//                else if (type.equals("2")) {
//                    Intent newIntent = new Intent(context, StoreActivity.class);
//                    intent.putExtra(EXTRA_ID, jsonobject.getString("id"));
//                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    context.startActivity(newIntent);
//                } else if (type.equals("3")) {
//                    Intent newIntent = new Intent(context, ArticleActivity.class);
//                    intent.putExtra(EXTRA_ID, jsonobject.getString("id"));
//                    intent.putExtra(EXTRA_TYPE, FindFragment.ARTICLE_TYPE_NEWS);
//                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    context.startActivity(newIntent);
//                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
